// CamSelect.cpp : implementation file
//

#include "stdafx.h"
#include "Resource.h"

#include "CamSelect.h"
#include "../RMSLib/Registry.h"
#include "../RMSLib/RMSEditCameraDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCamSelect dialog


CCamSelect::CCamSelect(ClientInterface* pClient, CWnd* pParent /*=NULL*/)
	: CDialog(CCamSelect::IDD, pParent)
{
	setClient(pClient);

	//{{AFX_DATA_INIT(CCamSelect)
	m_CurAddr = _T("");
	m_Ring = FALSE;
	//}}AFX_DATA_INIT
	char str[1024]={0};
	REGISTRY_LoadString(KEY_ADDR,str,1024);
	m_CurAddr=str;

	m_liveCamName = _T("live_cameras.txt");
	m_storedCamName = _T("live_cameras.txt");

#ifdef EXPIRED1
#else
//	m_Ring=REGISTRY_LoadInt(KEY_RING,1);
#endif
	InitialCam="";
}
void   CCamSelect::SetInitial(CString str)
{
	InitialCam=str;
}

BOOL CCamSelect::GetRing()
{
	return m_Ring;
}

void CCamSelect::SetLiveCamName(CString name)
{
	m_liveCamName = name;
}

FILE* CCamSelect::getLiveCamFile(LPCTSTR access) 
{
	FILE* f = NULL;

	CString hp = getHomePath();
	if(hp.IsEmpty())
	{
		f = fopen(m_liveCamName, access);
		if(!f)
		{
			//hp = "c:"; // default
			hp = ".";
			saveHomePath(hp);
		}
	}

	if(!f)
	{
		hp += "\\";
		hp += m_liveCamName;
		f = fopen(hp, access);
	}

	return f;
}

void CCamSelect::SetStoredCamName(CString name)
{
	m_storedCamName = name;
}

FILE* CCamSelect::getStoredCamFile(LPCTSTR access) 
{
	FILE* f = NULL;

	CString hp = getHomePath();
	if(hp.IsEmpty())
	{
		f = fopen(m_storedCamName, access);
		if(!f)
		{
			//hp = "c:"; // default
			hp = ".";
			saveHomePath(hp);
		}
	}

	if(!f)
	{
		hp += "\\";
		hp += m_storedCamName;
		f = fopen(hp, access);
	}

	return f;
}


void CCamSelect::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCamSelect)
	DDX_Control(pDX, IDC_LIST1, m_List);
	DDX_Text(pDX, IDC_CURADDR, m_CurAddr);
	DDX_Check(pDX, IDC_CHECK1, m_Ring);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCamSelect, CDialog)
	//{{AFX_MSG_MAP(CCamSelect)
	ON_LBN_DBLCLK(IDC_LIST1, OnDblclkList1)
	ON_BN_CLICKED(IDC_SEL_CUR, OnSelCur)
	ON_BN_CLICKED(IDC_CHECK1, OnEnRing)
	ON_BN_CLICKED(IDC_ORGANIZE, OnOrganize)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCamSelect message handlers

void CCamSelect::OnOK() 
{
	// TODO: Add extra validation here
	
	GetSelected();

	UpdateData(TRUE);
	REGISTRY_SaveInt(KEY_RING,m_Ring);

	CDialog::OnOK();
}

void CCamSelect::ReadAddressBook()
{
	m_List.ResetContent();

	//	Load Site, Building, Room, Camera Information and Init combobox
	char site[128];
	char building[128];
	char room[128];
	char camera[128];
	char password[128];
	int ID = 0;
	char ip[128];
	int port = 0;
	int FrameRate = 0;
	int QualityRange = 0;
	int Bitrate = 0;
	int Width = 0;
	int Height = 0;

	FILE* file_in = getLiveCamFile("r");

	if(file_in)
	{
		while(!feof(file_in))
		{
			char str[1024];
			strcpy(site,"none");strcpy(building,"none");strcpy(room,"none");
			strcpy(camera,"none");strcpy(password,"none");
			fscanf(file_in,"%s %s %s %s %i %s %s %i %i %i %i %i %i",site,building,room,camera,&ID,password,ip,&port,&FrameRate,&QualityRange,&Bitrate,&Width,&Height);
			if (strcmp(site,"none")==0) continue;
			sprintf(str,"%s %s %s %s",site,building,room,camera);
			m_List.AddString(str);
		}
		m_List.SetCurSel(0);
		fclose(file_in);
	}
}
BOOL CCamSelect::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here


#ifdef EXPIRED1
	GetDlgItem(IDC_CHECK1)->EnableWindow(0);
	GetDlgItem(IDC_SEL_CUR)->EnableWindow(0);

	GetDlgItem(IDC_CURADDR)->ShowWindow(0);
	GetDlgItem(IDC_ADDR1)->ShowWindow(0);
#endif

	ReadAddressBook();	

	if (InitialCam!="") {
		m_List.SelectString(0,InitialCam);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

CString CCamSelect::GetSelected()
{
	CString temp;
	if (::IsWindow(m_hWnd)) {
		UpdateData(TRUE);
		int index=m_List.GetCurSel();
		if (index>=0) {
			m_List.GetText(index, temp);
			sscanf(temp,"%s %s %s %s",Site,Building,Room,Camera);
		}	
	}else{
		temp.Format("%s %s %s %s",Site,Building,Room,Camera);
	}
	return temp;
}

void CCamSelect::OnDblclkList1() 
{
	// TODO: Add your control notification handler code here
	//GetSelected();
	OnOK();
	
}

void CCamSelect::OnSelCur() 
{
	// TODO: Add your control notification handler code here
	GetSelected();
	// TODO: Add extra initialization here

	//	Load Site, Building, Room, Camera Information and Init combobox
	char site[128];
	char building[128];
	char room[128];
	char camera[128];
	char password[128];
	int ID = 0;
	char ip[128];
	int port = 0;
	int FrameRate = 0;
	int QualityRange = 0;
	int Bitrate = 0;
	int Width = 0;
	int Height = 0;


	FILE* file_in = getLiveCamFile("r");

	if(file_in)
	{
		while(!feof(file_in))
		{
			strcpy(site,"none");strcpy(building,"none");strcpy(room,"none");
			strcpy(camera,"none");strcpy(password,"none");
			fscanf(file_in,"%s %s %s %s %i %s %s %i %i %i %i %i %i",site,building,room,camera,&ID,password,ip,&port,&FrameRate,&QualityRange,&Bitrate,&Width,&Height);
			if (strcmp(site,"none")==0) continue;

			if (
				strcmp(Site,site)==0 &&
				strcmp(Building,building)==0 &&
				strcmp(Room,room)==0 &&
				strcmp(Camera,camera)==0
			) {				
				
				m_CurAddr.Format("%s %s %s %s %i %s %s %i %i %i %i %i %i",site,building,room,camera,ID,password,ip,port,FrameRate,QualityRange,Bitrate,Width,Height);
				REGISTRY_SaveString(KEY_ADDR,m_CurAddr);
				UpdateData(FALSE);
				break;
			}
		}
	}
	if(file_in)
	fclose(file_in);
	
}

void CCamSelect::OnEnRing() 
{
	// TODO: Add your control notification handler code here
	
}

void CCamSelect::OnOrganize() 
{
	// TODO: Add your control notification handler code here
	GetSelected();

	CRMSEditCameraDlg dlg(getClient());

	dlg.SetLiveCamName(m_liveCamName);
	dlg.SelectCamera(Site,Building,Room,Camera);


	dlg.DoModal();
	ReadAddressBook();	
	CString sel=dlg.GetSelectedSite();
	m_List.SelectString(0,sel);
}
